/*
 * 介绍:tabbar页数据管理
 */
import { defineStore } from "pinia";
import { apiHomeData, HomeData } from "@@/api/module/tabbarPage";
import useBalanceStore from "@@/store/useBalanceStore";
import { _require } from "@@/utils/tools";

export default defineStore("TabPageStore", {
  //*全局仓库
  state: initState,
  //*全局函数
  actions: {
    async getHomeData() {
      const res = await apiHomeData();
      if (!res) return;
      this.$patch((state) => (state.homeData = res));
      const balanceStore = useBalanceStore();
      const storageCoin = res.storage_coin;
      balanceStore.ethCoinEnum[storageCoin.id] = {
        ...balanceStore.ethCoinEnum[storageCoin.id],
        logo: _require(
          `src/assets/images/coin/${storageCoin.code.toLowerCase()}.png`
        ),
        contract_addr: storageCoin.contract,
        format: storageCoin.decimal,
        id: storageCoin.id,
        code: storageCoin.code,
      };
      return res;
    },
  },
  //*计算属性
  getters: {},
  //*仓库数据缓存
  persist: [
    {
      key: "TabPageStore",
      storage: localStorage, //指定储存位置
    },
  ],
});
/**state类型定义 */
interface TabPageStore {
  homeData: HomeData.Res | undefined;
}

/**初始化pinia */
function initState(): TabPageStore {
  return {
    homeData: undefined,
  };
}
